import { Component, OnInit, Input } from '@angular/core';
import { UserCacheService } from '../user-cache.service';
import { User } from 'src/app/user';
import { HttpErrorResponse, HttpResponse } from '@angular/common/http';
import { throwError } from 'rxjs';

@Component({
  selector: 'app-identitya',
  templateUrl: './identitya.component.html',
  styleUrls: ['./identitya.component.css'],
  providers: [UserCacheService]
})
export class IdentityaComponent implements OnInit {

  @Input() user: User;
  constructor(private userCache: UserCacheService) {
    this.userCache.onchanged.subscribe(user => {
      this.user = user;
      console.log(`cache user changed.`);
    });
   }

  ngOnInit() {
    if (this.user) {
      this.userCache.setCacheUser(this.user);
    }
  }
  refreshUser() {
    this.userCache.setCacheUser(new User(this.user.id, `test_${new Date().getMilliseconds()}`, this.user.record));
  }
  testHttp() {
    const url = 'api/users';
    this.userCache.post({url: url, data: {id: 2}})
    .subscribe(result => {
      if (result) {
        console.log(result);
      } else {
        console.log(`request result failured`);
      }
    }, error => {
      console.log('testHttp has error');
    });
  }
}
